package com.example.waimai.contraller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.waimai.common.R;
import com.example.waimai.pojo.Category;
import com.example.waimai.service.ServiceCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryContraller {
    @Autowired
    private ServiceCategory serviceCategory;

    //增加
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("增加的分类为：{}",category);
        serviceCategory.save(category);
        return R.success("新增分类成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page> Getpage(int page,int pageSize){
        Page<Category> pageinfo=new Page(page,pageSize);//分页构造器
        LambdaQueryWrapper<Category> lwq=new LambdaQueryWrapper<>();
        lwq.orderByAsc(Category::getSort);
        Page page1 = serviceCategory.page(pageinfo, lwq);
        return R.success(page1);

    }

    //删除
    @DeleteMapping
    public R<String> delete(Long ids){
        serviceCategory.removeById(ids);
        return R.success("删除成功");
    }

    //修改
    @PutMapping
    public R<String> updata(@RequestBody Category category){
        log.info("修改的信息:{}",category);
        serviceCategory.updateById(category);
        return R.success("修改成功");
    }

    //增加菜品
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> lwq=new LambdaQueryWrapper<>();
        lwq.eq(category.getType()!=null,Category::getType,category.getType());

        lwq.orderByDesc(Category::getCreateTime);
        List<Category> list = serviceCategory.list(lwq);

        return R.success(list);
    }
}
